package com.zhitong.ztblog.controllers;

import com.zhitong.ztblog.entity.Blog;
import com.zhitong.ztblog.entity.PageInfo;
import com.zhitong.ztblog.services.IBlogService;
import com.zhitong.ztblog.services.ILabelService;
import com.zhitong.ztblog.utils.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
public class BlogContro {

    @Autowired
    private IBlogService blogService;

    @Autowired
    private ILabelService labelService;


    //首页博客分页
    @GetMapping("/blogs/{currentPage}")
    public AjaxResponse getAllBlog(@PathVariable(name = "currentPage") int currentPage){
        //当前页,页码数,总数
        PageInfo pageInfo=new PageInfo(currentPage,10,blogService.findBlogsNum());
        Blog[] all = blogService.findByPage(pageInfo);

        Map<String,Object> map=new HashMap<>();
        map.put("blogs",all);
        map.put("pageInfo",pageInfo);

        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setMessage("查询成功");
        ajaxResponse.setData(map);
        return ajaxResponse;
    }
    //首页博客分页
    @GetMapping("/blogs/{currentPage}/{pageSize}")
    public AjaxResponse getAllBlog(@PathVariable(name = "currentPage") int currentPage,@PathVariable(name = "pageSize") int pageSize){
        //当前页,页码数,总数
        PageInfo pageInfo=new PageInfo(currentPage,pageSize,blogService.findBlogsNum());
        Blog[] all = blogService.findByPage(pageInfo);

        Map<String,Object> map=new HashMap<>();
        map.put("blogs",all);
        map.put("pageInfo",pageInfo);

        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setMessage("查询成功");
        ajaxResponse.setData(map);
        return ajaxResponse;
    }

    //浏览量前五的博客
    @GetMapping("/blogTop")
    public AjaxResponse getTop5(){
        Blog[] top = blogService.findByTop();
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setMessage("查询成功");
        ajaxResponse.setData(top);
        return ajaxResponse;
    }

    //根据id查询博客,用以查看博客详情
    @GetMapping("/blog/{id}")
    public AjaxResponse getById(@PathVariable(name = "id") int id){
        Blog blog = blogService.findById(id);
        String[] labels = labelService.findNamesByBid(id);
        blog.setLabels(labels);

        AjaxResponse success = AjaxResponse.success(blog);
        return success;
    }

    @PutMapping("/blogTotle/{id}")
    public void updateTotle(@PathVariable(name = "id")int id){
        blogService.updateTotle(id);
    }

//    根据分类id和分页号
    @GetMapping("/sblogs/{sid}/{currentPage}")
     public AjaxResponse getSortBlog(@PathVariable(name="sid")int sid,@PathVariable(name = "currentPage") int currentPage){
        //当前页,页码数,总数
        PageInfo pageInfo=new PageInfo(currentPage,7,blogService.findBlogNumBySid(sid));
        Blog[] all = blogService.findBlogBySid(sid,pageInfo);

        Map<String,Object> map=new HashMap<>();
        map.put("blogs",all);
        map.put("pageInfo",pageInfo);

        AjaxResponse success = AjaxResponse.success(map);
        return success;
    }

    //    根据标签id和分页号
    @GetMapping("/lblogs/{lid}/{currentPage}")
    public AjaxResponse getlabelBlog(@PathVariable(name="lid")int lid,@PathVariable(name = "currentPage") int currentPage){
        //当前页,页码数,总数
        PageInfo pageInfo=new PageInfo(currentPage,7,labelService.findBlogNumByLid(lid));
        Blog[] all = blogService.findBlogByLid(lid,pageInfo);

        Map<String,Object> map=new HashMap<>();
        map.put("blogs",all);
        map.put("pageInfo",pageInfo);
        AjaxResponse success = AjaxResponse.success(map);
        return success;
    }

    //根据id删除博客
    @DeleteMapping("/blog/{id}")
    public AjaxResponse delBlogById(@PathVariable(name = "id")int id){
        blogService.delBlogById(id);
        AjaxResponse success = AjaxResponse.success();
        return success;
    }

    //插入博客
    @PostMapping("/blog")
    public AjaxResponse insertBlog(@RequestBody Blog blog){
        blogService.insertBlog(blog);
        AjaxResponse success = AjaxResponse.success();
        success.setMessage("上传成功");
        return success;
    }

    @PutMapping("/blog")
    public AjaxResponse updateBlog(@RequestBody Blog blog){
        blogService.updateBlog(blog);
        AjaxResponse success = AjaxResponse.success();
        success.setMessage("更新成功");
        return success;
    }

    @CrossOrigin
    @GetMapping("/blog/{word}/{currentPage}/{pageSize}")
    public AjaxResponse getBlogByWord(@PathVariable(name = "word") String word, @PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize) throws UnsupportedEncodingException {
        PageInfo pageInfo=new PageInfo(currentPage,pageSize,blogService.findBlogNumByWord(word));
//        String decode = URLDecoder.decode(word, "UTF-8");
        Blog[] blogs = blogService.findBlogByWord(word, pageInfo);
        Map<String,Object> map=new HashMap<>();
        map.put("blogs",blogs);
        map.put("pageInfo",pageInfo);
        AjaxResponse success = AjaxResponse.success(map);
        return success;
    }
}
